package actions;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.StuCourse;
import db.dao.StuCourseDAO;
import db.dao.StuCourseId;
import db.dao.Student;
import db.dao.StudentDAO;
import db.dao.TeaCourse;
import db.dao.TeaCourseDAO;
import db.dao.TeaCourseId;
import db.dao.Users;
import db.dao.UsersDAO;

/*学生选课，教师登成绩*/
public class UpdateStuCourseAction extends ActionSupport{
	private String ctid="";	//课程教师编号
	private String cid="";	//课程编号
	private String sid="";	//学生编号
	private String tid="";	//教师编号
	private String[] stuid;	//学生编号
	private String[] courseid;	//课程编号
	private String[] score;	//成绩
	
	public UpdateStuCourseAction() {
	}
	
	public String getCtid() {
		return ctid;
	}

	public void setCtid(String ctid) {
		this.ctid = ctid;
	}

	public String getCid() {
		return cid;
	}
	
	public void setCid(String cid) {
		this.cid = cid;
	}
	
	public String getSid() {
		return sid;
	}
	
	public void setSid(String sid) {
		this.sid = sid;
	}
	
	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}

	
	public String[] getStuid() {
		return stuid;
	}

	public void setStuid(String[] stuid) {
		this.stuid = stuid;
	}

	public String[] getCourseid() {
		return courseid;
	}

	public void setCourseid(String[] courseid) {
		this.courseid = courseid;
	}

	public String[] getScore() {
		return score;
	}

	public void setScore(String[] score) {
		this.score = score;
	}

	public String execute() throws IOException {
		//获取页面信息用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		//用于获取结果
		String result="";
		int i=0,j=0;
		UsersDAO uDao = new UsersDAO();
		Users users = uDao.findById((String) session.getAttribute("username"));
		tid = users.getUsername();
		if (users.getUsertype().equals("教师")) {
			//教师登成绩
			StuCourseDAO scDao = new StuCourseDAO();
			StuCourse stuCourse = new StuCourse();
			StuCourseId sCourseId = new StuCourseId();
			TeaCourseId tCourseId = new TeaCourseId();
			TeaCourseDAO tcDao = new TeaCourseDAO();
			TeaCourse teaCourse = new TeaCourse();
			StudentDAO sDao = new StudentDAO();
			Student student = new Student();
			for (j = 0; j < score.length; j++) {
				student = sDao.findById(stuid[j]);
				tCourseId.setCourseid(courseid[j]);
				tCourseId.setTeaid(tid);
				teaCourse = tcDao.findById(tCourseId);
				sCourseId.setCourseid(courseid[j]);
				sCourseId.setStuid(stuid[j]);
				sCourseId.setTeacher(tid);
				stuCourse = scDao.findById(sCourseId);
				if (!score[j].equals("")) {
					stuCourse.setScore(Short.parseShort(score[j]));
				}
				stuCourse.setStudent(student);
				stuCourse.setTeaCourse(teaCourse);
				result = scDao.save(stuCourse);
				if (result.equals("failed")) {
					i++;
				}
			}
		}else {
			//学生选课
			sid = users.getUsername();
			cid = ctid.split(";")[0];
			tid = ctid.split(";")[1];
			StudentDAO sDao = new StudentDAO();
			Student student = sDao.findById(sid);
			TeaCourseDAO tcDao = new TeaCourseDAO();
			TeaCourse teaCourse = new TeaCourse();
			TeaCourseId teaCourseId = new TeaCourseId();
			teaCourseId.setCourseid(cid);
			teaCourseId.setTeaid(tid);
			teaCourse = tcDao.findById(teaCourseId);
			StuCourseDAO scDao = new StuCourseDAO();
			StuCourse stuCourse = new StuCourse();
			StuCourseId sCourseId = new StuCourseId();
			sCourseId.setCourseid(cid);
			sCourseId.setStuid(sid);
			sCourseId.setTeacher(tid);
			stuCourse.setId(sCourseId);
			stuCourse.setStudent(student);
			stuCourse.setTeaCourse(teaCourse);
			//保存
			result = scDao.save(stuCourse);
		}
		if (result.equals("success")) {
			//成功弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            UsersDAO uDao1 = new UsersDAO();
            Users users1 = uDao1.findById((String)session.getAttribute("username"));
            if (users1.getUsertype().equals("教师")) {
            	printWriter.print("<script>alert('"+i+"个添加失败,"+(j-i)+"个添加成功!')</script>");
            }else{
            	printWriter.print("<script>alert('添加成功!')</script>");
            }
            /*if (users1.getUsertype().equals("管理")) {
				printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
			}else */if (users1.getUsertype().equals("教师")) {
				printWriter.print("<script>window.location.href='/GroupProject/teacher.jsp'</script>");
			}else {
				printWriter.print("<script>window.location.href='/GroupProject/student.jsp'</script>");
			}
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}else {
			//失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            UsersDAO uDao1 = new UsersDAO();
            Users users1 = uDao1.findById((String)session.getAttribute("username"));
            if (users1.getUsertype().equals("教师")) {
            	printWriter.print("<script>alert('"+i+"个添加失败,"+(j-i)+"个添加成功!')</script>");
            }else{
            	printWriter.print("<script>alert('添加失败!')</script>");
            }
            /*if (users1.getUsertype().equals("管理")) {
				printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
			}else */if (users1.getUsertype().equals("教师")) {
				printWriter.print("<script>window.location.href='/GroupProject/teacher.jsp'</script>");
			}else {
				printWriter.print("<script>window.location.href='/GroupProject/student.jsp'</script>");
			}
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
